﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports MySql.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Threading
Imports MySql.Data.MySqlClient

Public Class frmLogin
    Public Shared acesso As Integer = 0
    Public Shared nome As String, usuario As String
    Private erro As Boolean = True

    Private Sub tmBP_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles tmBP.Tick
        pbCarregar.Increment(1)
        If pbCarregar.Value = 100 Then
            Dim inicio As New frmMain()
            'inicio.Show()
            Me.Hide()
            Thread.Sleep(30)
            Animacao(inicio)
            txtUser.Text = ""
            txtPass.Text = ""
            tmBP.Stop()
        End If
    End Sub

    Private Sub frmLogin_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        ' Try
        ' Define entrada de texto para maiúsculas.
        txtUser.CharacterCasing = CharacterCasing.Upper
        Me.AcceptButton = btnLogin
        'Me.Opacity = 0
        ' Dim cont As Double = 0
        ' While cont <= 1
        'Me.Opacity = cont
        'Me.Refresh()
        'Thread.Sleep(15)
        'cont += 0.1
        'End While
        Animacao(Me)
        'Catch erro As Exception
        '   MsgBox(erro.Message, vbOKOnly, "Erro")
        '  End
        'End Try
    End Sub

    Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
        If txtUser.Text = "" OrElse txtPass.Text = "" Then
            lblStatus.Text = "Usuário e/ou senha inválidos."
        Else
            Dim SCE As New MySqlConnection(My.MySettings.Default.SCE_ETEC_ConnectionString)
            Dim cmd As New MySqlCommand()
            Dim sql As String = "SELECT nome, usuario, senha, nivel_acesso FROM Login nolock where usuario='" & txtUser.Text & "' and senha='" & txtPass.Text & "'"
            Dim Login As New DataTable()

            Try
                cmd = New MySqlCommand(sql, SCE)
                SCE.Open()
                cmd.CommandType = CommandType.Text

                Dim SCEAdapter As New MySqlDataAdapter(cmd)
                SCEAdapter.Fill(Login)
            Catch ex As Exception
                If MsgBox("Erro: " & ex.Message & vbCrLf & vbCrLf & "Deseja que o programa tente reparar a conexão com o servidor?", vbYesNo, "Erro") = vbYes Then
                    txtUser.Enabled = False
                    txtPass.Enabled = False
                    btnLogin.Enabled = False
                    btnSair.Enabled = False
                    lblStatus.Text = "Aviso: Reparando conexão." & vbCrLf & "Favor aguardar..."
                    bwReparar.RunWorkerAsync()
                End If
                Exit Sub
            Finally
                SCE.Close()
            End Try

            If Login.Rows.Count = 1 Then
                acesso = Convert.ToInt32(Login.Rows(0).Item(3).ToString)
                usuario = Login.Rows(0).Item(1).ToString
                nome = Login.Rows(0).Item(0).ToString
                tmBP.Start()
                txtUser.Enabled = False
                txtPass.Enabled = False
                btnLogin.Enabled = False
                btnSair.Enabled = False
            Else
                txtUser.Enabled = True
                txtPass.Enabled = True
                btnLogin.Enabled = True
                btnSair.Enabled = True
                lblStatus.Text = "Usuário e/ou senha inválidos."
            End If
        End If
    End Sub

    Private Sub btnSair_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSair.Click
        End
    End Sub

    Private Sub txtUser_Click(ByVal sender As Object, ByVal e As EventArgs) Handles txtUser.Click
        lblStatus.Text = ""
    End Sub

    Private Sub txtPass_Click(ByVal sender As Object, ByVal e As EventArgs) Handles txtPass.Click
        lblStatus.Text = ""
    End Sub

    Private Sub bwReparar_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwReparar.DoWork
        Dim SCE As New MySqlConnection(My.MySettings.Default.SCE_ETEC_ConnectionString)

        Try
            Shell(Application.StartupPath.Substring(0, 2) & "\xampp\mysql_start.bat", AppWinStyle.Hide)
            Thread.Sleep(5000)

            SCE.Open()
            erro = False
        Catch ex As Exception
            erro = True
        Finally
            SCE.Close()
        End Try
    End Sub

    Private Sub bwReparar_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwReparar.RunWorkerCompleted
        If erro Then
            lblStatus.Text = "Erro: conexão não reparada."
            MsgBox("Não foi possivel reparar a conexão com o servidor, favor contactar o Suporte.", vbOKOnly, "Aviso")
            txtUser.Enabled = True
            txtPass.Enabled = True
            btnLogin.Enabled = True
            btnSair.Enabled = True
        Else
            lblStatus.Text = "Aviso: Conexão reparada." & vbCrLf & "Efetuando login ..."
            InvokeOnClick(btnLogin, EventArgs.Empty)
        End If
    End Sub
End Class
